<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="商品编号" prop="number">
                    <el-input v-model="formData.number" clearable placeholder="请输入商品名称" />
                </el-form-item>
                <el-form-item label="商品全名" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入商品名称" />
                </el-form-item>
                <el-form-item label="商品单位" prop="unit">
                    <el-input v-model="formData.unit" clearable placeholder="请输入规格" />
                </el-form-item>
                <el-form-item label="数量" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入下单数量" />
                </el-form-item>
                <el-form-item label="单价" prop="price">
                    <el-input v-model="formData.price" clearable placeholder="请输入下单数量" />
                </el-form-item>
                <el-form-item label="来源" prop="type">
                    <el-select v-model="formData.type" placeholder="来源">
                        <el-option label="美菜" :value="0"></el-option>
                        <el-option label="快驴" :value="1"></el-option>
                        <el-option label="优大集" :value="3"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="skuMeicaiEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSkuMeicaiAdd, apiSkuMeicaiEdit, apiSkuMeicaiDetail } from '@/api/sku_meicai'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑存储导入表' : '新增存储导入表'
})

// 表单数据
const formData = reactive({
    id: '',
    number: '',
    name: '',
    unit: '',
    num: '',
    price: '',
    type: 1
})

// 表单验证
const formRules = reactive<any>({
    number: [
        {
            required: true,
            message: '请输入商品编号',
            trigger: ['blur']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入商品全名',
            trigger: ['blur']
        }
    ],
    unit: [
        {
            required: true,
            message: '请输入商品单位',
            trigger: ['blur']
        }
    ],
    num: [
        {
            required: true,
            message: '请输入数量',
            trigger: ['blur']
        }
    ],
    price: [
        {
            required: true,
            message: '请输入单价',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请选择来源',
            trigger: ['change']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }

    //@ts-ignore
    formData.orderTime = timeFormat(formData.orderTime, 'yyyy-mm-dd hh:MM:ss')
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiSkuMeicaiDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit' ? await apiSkuMeicaiEdit(data) : await apiSkuMeicaiAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
